//
//  XLPlayer.m
//  FFMpegKitDemo
//
//  Created by ilongge on 2023/8/2.
//

#import "XLPlayer.h"
#import "XLPlayerParseHandler.h"
#import "XLPlayerItem.h"
#import "XLPlayerConfig.h"
#import "XLPlayerError.h"
#include <syslog.h>

@interface XLPlayer()
@property (nonatomic, strong) XLPlayerParseHandler *parseHandler;
@property (nonatomic, assign) BOOL readyToPlay;
@end

@implementation XLPlayer

#pragma mark ------------------------- 初始化 / Initializer

+ (instancetype)playerWithURL:(NSURL *)URL config:(XLPlayerConfig *)config error:(NSError *__autoreleasing *)error
{
    XLPlayer *player = [[XLPlayer alloc] initWithURL:URL config:config error:error];
    return player;
}

- (instancetype)initWithURL:(NSURL *)URL config:(XLPlayerConfig *)config error:(NSError *__autoreleasing *)error
{
    if (URL == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlNil];
        return nil;
    }
    else if (URL.path == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlInvalid];
        return nil;
    }
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        NSError *_error = *error;
        _url = URL;
        _item = [[XLPlayerItem alloc] initWithURL:URL error:&_error];
        if (config == nil) {
            config = [[XLPlayerConfig alloc] init];
        }
        _config = config;
    }
    return self;
}

+ (instancetype)playerWithPlayerItem:(XLPlayerItem *)item config:(XLPlayerConfig *)config error:(NSError *__autoreleasing *)error
{
    XLPlayer *player = [[XLPlayer alloc] initWithPlayerItem:item config:config error:error];
    return player;
}

- (instancetype)initWithPlayerItem:(XLPlayerItem *)item config:(XLPlayerConfig *)config error:(NSError *__autoreleasing *)error
{
    if (item == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeItemNil];
        return nil;
    }
    else if (item.url == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlNil];
        return nil;
    }
    else if (item.url.path == nil) {
        *error = [XLPlayerError errorWithCode:XLPlayerErrorCodeUrlInvalid];
        return nil;
    }
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        _item = item;
        _url = item.url;
        if (config == nil) {
            config = [[XLPlayerConfig alloc] init];
        }
        _config = config;
    }
    return self;
}


#pragma mark ------------------------- 公开方法 / Public

- (UIImage *)screenshot:(CGSize)imageSize
{
    return nil;
}

- (BOOL)prepareToPlayError:(NSError *__autoreleasing *)error
{
    _readyToPlay = NO;
    NSError *_error = *error;
    _parseHandler = [XLPlayerParseHandler parseHandlerWithURL:_url
                                                        error:&_error];
    if (_error) {
        *error = _error;
        return _readyToPlay;
    }
    [_parseHandler prepareParseError:&_error];
    if (_error) {
        *error = _error;
        return _readyToPlay;
    }
    _readyToPlay = YES;
    return _readyToPlay;
}

- (void)play
{
    if (_readyToPlay) {
        [_parseHandler startParse:^(BOOL isVideo, AVFrame *frame) {
            NSLog(@"");
        }];
    }
    else{
        NSLog(@"播放出错");
    }
}

- (void)pause
{
    [_parseHandler readNext:NO];
}

- (void)close
{
    
}

#pragma mark ------------------------- 内部方法 / Private

@end
